<template>
  <div class="g-inherit m-article p-chat-history">
    <div class="top-header border-bottom fixed-top" style="z-index:100">
      <div class="top-back">
        <a href="javascript:history.go(-1)">返回</a>
      </div>
      <h2 class="f36 color-000">{{sessionName}}</h2>
    </div>
    <div class="m-chat-main">
      <chat-list
        ref="chatlist"
        type="session"
        :canLoadMore="canLoadMore"
        :msglist="msglist"
        :userInfos="userInfos"
        :myInfo="myInfo"
        :robotInfos="robotInfos"
        v-touch:swipedown="loadMore"
      ></chat-list>
    </div>
  </div>
</template>

<script>
import ChatList from './components/ChatList'
import util from 'js/utils'
import pageUtil from 'js/utils/page'

export default {
  beforeMount () {
    // 如果是刷新页面，重定向至聊天页面
    if (this.$store.state.im.isRefresh) {
      //location.href = `/chat/${this.sessionId}`
      this.$router.go(-1)
    }
  },
  mounted () {
    // this.$store.dispatch('initNimSDK', {
    //   uid: 'ldq',
    //   //sdktoken: '35f77d313aad5695068e16f0a3dd9790'
    //   sdktoken:'8f418dfb7c2c7a90c8a200729174919d'
    // })
    this.$store.dispatch('resetNoMoreHistoryMsgs')
    //this.getHistoryMsgs()
  },
  updated () {
    let tempPagePos = pageUtil.getChatListHeight()
    pageUtil.scrollChatListDown(tempPagePos - this.currPagePos)
    this.currPagePos = tempPagePos
  },
  watch:{
    sessionId(){
      this.getHistoryMsgs()
    }
  },
  components: {
    ChatList
  },
  data () {
    return {
      leftBtnOptions: {
        backText: ' ',
        preventGoBack: true,
      },
      currPagePos: 0,
      // selectedDate: ''
    }
  },
  computed: {
    sessionId () {
      return this.$route.params.sessionId.replace('%','@5173.com')
    },
    sessionName () {
      let sessionId = this.sessionId
      let user = null
      if (/^p2p-/.test(sessionId)) {
        user = sessionId.replace(/^p2p-/, '')
        if (user === this.$store.state.im.userUID) {
          return '我的手机'
        } else if (this.isRobot) {
          return this.robotInfos[user].nick || user
        } else {
          let userInfo = this.userInfos[user] || {}
          return util.getFriendAlias(userInfo)
        }
      } else if (/^team-/.test(sessionId)) {
        return '群'
      }
    },
    // 判断是否是机器人
    isRobot () {
      let sessionId = this.sessionId
      let user = null
      if (/^p2p-/.test(sessionId)) {
        user = sessionId.replace(/^p2p-/, '')
        if (this.robotInfos[user]) {
          return true
        }
      }
      return false
    },
    myInfo () {
      return this.$store.state.im.myInfo
    },
    userInfos () {
      return this.$store.state.im.userInfos
    },
    msglist () {
      let msgs = this.$store.state.im.currSessionMsgs
      return msgs
    },
    robotInfos () {
      return this.$store.state.im.robotInfos
    },
    scene () {
      return util.parseSession(this.sessionId).scene
    },
    to () {
      return util.parseSession(this.sessionId).to
    },
    canLoadMore () {
      return !this.$store.state.im.noMoreHistoryMsgs
    }
  },
  methods: {
    getHistoryMsgs () {
      if (this.canLoadMore) {
        this.$store.dispatch('getHistoryMsgs', {
          scene: this.scene,
          to: this.to
        })
      }
    },
    loadMore () {
      if (pageUtil.getChatListScroll() <= 5) {
        this.getHistoryMsgs()
      }
    },
    onClickBack: function () {
      // location.href = `#/chat/${this.sessionId}`
      window.history.go(-1)
    }
  }
}
</script>

<style scoped>
  .p-chat-history {
    .m-chat-main {
      padding: 0;
    }
  }
</style>
<style  src="css/member/member-im.css"></style>
